package guda.house.biz.impl;

import java.util.List;

import guda.house.biz.constants.CommonResultCode;
import guda.house.biz.query.RoleResourceQuery;
import guda.house.dal.dao.RoleResourceRelDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import guda.house.biz.ResourceBiz;
import guda.house.dal.dao.ResourceDAO;
import guda.house.dal.dataobject.ResourceDO;
import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;

public class ResourceBizImpl implements ResourceBiz{

    private final static Logger logger = LoggerFactory.getLogger(ResourceBizImpl.class);

    @Autowired
    private ResourceDAO resourceDAO;
    @Autowired
    private RoleResourceRelDAO roleResourceRelDAO;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            ResourceDO resourceDO = resourceDAO.getResourceById(id);
            bizResult.data.put("resourceDO", resourceDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query Resource error",e);
        }
        return bizResult;
}

    public BizResult list(BaseQuery baseQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = resourceDAO.countForPage(baseQuery);
            baseQuery.setTotalCount(totalCount);
            List<ResourceDO> resourceList = resourceDAO.selectForPage(baseQuery);
            bizResult.data.put("resourceList", resourceList);
            bizResult.data.put("query", baseQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view Resource list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            //如果有角色关联该资源，是不允许删除的
            RoleResourceQuery roleResourceQuery = new RoleResourceQuery();
            roleResourceQuery.setResourceId(id);
            int i = roleResourceRelDAO.countForPage(roleResourceQuery);
            if(i>0){
                bizResult.code = CommonResultCode.RESOURCE_CANNOT_DEL_LINK_BY_ROLE;
                return bizResult;
            }
            resourceDAO.delResourceById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete resource error", e);
        }
        return bizResult;
    }

    public BizResult create(ResourceDO resourceDO) {
        BizResult bizResult = new BizResult();
        try {
            long id = resourceDAO.insert(resourceDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create Resource error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(ResourceDO resourceDO) {
        BizResult bizResult = new BizResult();
        try {
            resourceDAO.updateByIdSelective(resourceDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update Resource error", e);
        }
        return bizResult;
    }


}
